package com.demo.view;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.GridView;


import androidx.annotation.NonNull;

import com.base.KeyBoardAdapter;
import com.tg.appcommon.android.TGLog;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class VirtualKeyboardViewHelper {
    private VirtualKeyboardView virtualKeyboardView;
    private ArrayList<Map<String, String>> valueList;
    private int id;

    public void setCurEditText(String curEditText) {
        this.curEditText = curEditText;
    }

    private String curEditText = "";
    public VirtualKeyboardViewHelper(){
        initValueList();
    }

    public void showLayoutBack(){
        if (virtualKeyboardView != null){
            virtualKeyboardView.getLayoutBack().setVisibility(View.VISIBLE);
            virtualKeyboardView.getLayoutBack().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    virtualKeyboardView.setVisibility(View.GONE);
                }
            });
        }
    }
    public void hideSoftInputMode(@NonNull Activity activity, @NonNull EditText editText){
        // 设置不调用系统键盘
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        try {
            Class<EditText> cls = EditText.class;
            Method setShowSoftInputOnFocus;
            setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",
                    boolean.class);
            setShowSoftInputOnFocus.setAccessible(true);
            setShowSoftInputOnFocus.invoke(editText, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showVirtualKeyboardView(int id){
        this.id = id;
        virtualKeyboardView.setFocusable(true);
        virtualKeyboardView.setFocusableInTouchMode(true);
        virtualKeyboardView.setVisibility(View.VISIBLE);
    }

    public void hideVirtualKeyboardView(){
        virtualKeyboardView.setFocusable(false);
        virtualKeyboardView.setFocusableInTouchMode(false);
        virtualKeyboardView.setVisibility(View.GONE);
    }


    public void initVirtualKeyboardView(Context context, VirtualKeyboardView virtualKeyboardView, final OnItemClickListener listener){
        this.virtualKeyboardView = virtualKeyboardView;
        GridView gridView = virtualKeyboardView.getGridView();
        initValueList();

        // 这里、重新为数字键盘gridView设置了Adapter
        KeyBoardAdapter keyBoardAdapter = new KeyBoardAdapter(context, valueList);
        gridView.setAdapter(keyBoardAdapter);
        gridView.setOnItemClickListener((parent, view3, position, id) -> {
            TGLog.d("position = " + position);
            String val = valueList.get(position).get("name");
            TGLog.d("val = " + val);
            if (position < 11 && position != 9) {    //点击0~9按钮
                if (curEditText.length() < 8){
                    curEditText += val;
                }
            } else {
                if (position == 11) {      //点击退格键
                    if (curEditText.length() > 0){
                        curEditText = curEditText.substring(0, curEditText.length()-1);
                    }
                }
            }
            if (listener != null){
                listener.onClickResult(this.id, curEditText);
            }
        });
    }

    // 这里，我们没有使用默认的数字键盘，因为第10个数字不显示.而是空白
    private void initValueList() {
        if (valueList == null){
            valueList = new ArrayList<>();

            // 初始化按钮上应该显示的数字
            for (int i = 1; i < 13; i++) {
                Map<String, String> map = new HashMap<String, String>();
                if (i < 10) {
                    map.put("name", String.valueOf(i));
                } else if (i == 10) {
                    map.put("name", "");
                } else if (i == 11) {
                    map.put("name", String.valueOf(0));
                } else if (i == 12) {
                    map.put("name", "");
                }
                valueList.add(map);
            }
        }

    }

    public interface OnItemClickListener{
        void onClickResult(int id, String ret);
    }

}
